\chapter{Methodology for JavaPict}
This chapter explains certain implementation details and optimizations made. Also, brief description of all the tools used is mentioned in this chapter.
\section{$\pi$-calculus and object oriented paradigm}
The main components of the $\pi$ calculus environment are processes and channels. Process and Channel are modeled as separate Java classes. According to Milner\cite{Milner_book}, the interaction between two processes via a channel can be viewed as a message passing event. This is captured in the object-oriented system as a method call to send and receive a message. 
Processes and channels can be modeled as objects with special features each. %Channels are synchronous. %Synchronization can be achieved either by using semaphores and locks or software transactional memory(STM). 
In order to simplify the implementation, sending channel along a channel can be modeled by allowing Process to switch connection from one channel to another. For instance, in example 3.1.4, we want to send channel 'a' across 'b', hence we grant the permission to S to change the connection of Channel 'a' from S to C1. \\
Every class with superclass Process has a method named run(). Thus the event of running a process can be triggered by calling the method run(). Each class whose superclass is Process stores a data structure to store the two processes which it connects. \\
All operations on channel have to be performed synchronously, since at any point of time a channel can be handled by two processes and hence can lead to race conditions. The use of locks and semaphores can solve this problem. But the use of locks is very messy in a way that the programmer has to focus more on avoiding race conditions and other implementation and algorithmic details get neglected. Also, use of locks makes the system very slow. Software transactional Memory(STM) is an alternative to lock-based synchronization. It allows programmers to focus on parallelism rather than maintaining correctness and avoiding deadlock. The section below gives a brief overview about the theory behind STM and its benefits.
\section{Software Transactional Memory(STM)} 
Multicore technology is advancing at a faster pace and parallel programming is the need of the day. Concurrent programming using locks and condition variables dates back to 30 years. They suffer from race condition due to misplaced or forgotton locks. If locks are acquired in wrong order, they can lead to deadlock. Use of locks can also lead to lost wakeups.\\
In 2005, Tim Harris, Simon Peyton Jones and Maurice Herlihy came up with the theory of composable memory transactions. The solution was initially suggested for hardware transactional memory which was later proposed to be implemented in software. This theory was called software transactional memory.\\ 
Software transactional memory can perform groups of operations atomically. It is like writing the sequential code and enclosing the part you want to run atomically in the $atomic$ block. Transactional memory provides freedom from deadlock and priority inversion. On timeout the program undergoes automatic rollback to its initial state. \\
The STM implementation for Haskell is most widely used and studied. However work has been done in other languages as well. DeuceSTM is a library for STM in JAVA. \\
In STM transaction is analogous to the database transactions for controlling access to a shared memory space. STM allows the following operations :
\begin{itemize}
 \item Atomic - Individual operations enclosed in atomic block are treated as a single transaction
 \item Retry - Aborts the transaction and restarts it from the beginning
 \item do-orelse - do s$_1$ orelse s$_2$ will try to run s$_1$ first and if it fails or retries, it runs s$_2$. If s$_2$ also retries then the complete call to the function retries. 
\end{itemize}
For understanding the working in a better way, consider the following example which employs deuceSTM for concurrency handling:\\
\begin{verbatim}
 public class BankAccount{
  public int id; 
  public double amount;
  public BankAccount(int id, double amount){
    this.id = id; this.amount = amount;
  }
 }
 
 public class Bank{
  @Atomic(retries=10)
  public void transfer(BankAccount acc1, BankAccount acc2, double amount){
    acc1.amount -= amount;
    acc2.amount += amount;
  }
 }
\end{verbatim}
Suppose a$_1$ and a$_2$ are two bank accounts. The function {\it transfer} is annotated with the {\it @Atomic} annotation, which will ensure that the function will run atomically. Only when all the three variables in the function call are not being modified by an atomic block, will this transaction proceed and once it has proceeded it will lead to completion. The method will retry for 10 times. \\
More about STM and deuce can be read in \cite{stm} and \cite{deuce} respectively. 
\section{JavaPict library}
Using the above STM tool and the object oriented abstraction, the following equivalence can be established.\\
P is a process, 'a', 'b' represent channels and m$_1$, m$_2$ represent messages. 
\begin{center}
\begin{tabular}{ll}
 Empty Process \{0\} & NULL \\
 Input Process \{a(m)P\} & P.send(a, m)\\
 Output Process \{$\bar{a}<m>$.P\} & P.receive(a, m)\\
 Scpoe \{(vx)P\} & local variable in P\\
 Choice \{(P+Q)\} & choice(P, Q)\\
 Parallel \{P $\|$ Q\} & parallel(P, Q)
\end{tabular}
\end{center}

This implementation of JavaPict does not handle the replication operator. Each channel is connected to processes and sending or receiving should be done synchronously. Hence the $send$ and $receive$ methods are enclosed in atomic block. \\ 
\section{Usage}
A project using JavaPict consists mainly of class declarations, wherein each class which extends the base class Process is also a process and all the work that the process does is implemented in the method $run()$. A class which extends Channel is also a Channel. A UI is also provided which gives an overview of the environment. Processes are represented as rectangles, and channels as straight lines. Figure @@@ shows the a work-flow environment which employs $\pi$ calculus constructs. \\
\begin{figure}
\centering
\includegraphics[scale=0.5]{figures/ui_workflow.pdf}
\caption{Work-Flow example with JavaPict}
\end{figure}